<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.2.39/vue.global.min.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" name="" v-model="connectURL" id=""> <button @click="connect">连接</button>
        <div>
            <li v-for="(v,i) in list" :key="i">
                {{v}}
            </li>
        </div>
        <hr>
        接收用户id：<input type="text" name="" v-model="recvid" id=""><br>
        <textarea name="" id="" cols="30" rows="10" v-model="txt"></textarea><br>
        <button @click="sendmsg">发送SEND</button>
        <button @click="ack">收到ACK</button>

        <hr>
        <br>
        <input type="text" name="" v-model="group" id="">
        <button @click="joingroup">加入组</button>
        <button @click="leftgroup">离开组</button>
        <button @click="sendtogroup">发送信息到组</button>


        <br>
        <hr>

        登录用户id：
        {{ uid }}
    </div>
    <script>
        const { createApp } = Vue
        var wsclient = null
        var onmessage=function(ev){
            let msg = JSON.parse(ev.data)
            if(msg.Type=="notify_id"){
                app.uid=msg.Msg
                app.list.splice(0,0,"连接时间 "+(new Date()).toString())
            }
            if(msg.Type=="receive"){
                app.list.splice(0,0,msg.SendId+" ----> "+msg.Msg.Msg)
            }
            if(msg.Type=="notify_ack"){
                app.list.splice(0,0,msg.SendId+" 表示收到你的信息")
            }
            console.log(msg)
        }
        var onclose=function(){
            alert('退出了')
            app.uid=''
            // setTimeout(() => {
            //     //3s重连一次
            //     wsclient = new WebSocket("ws://127.0.0.1:9999/ws")
            //     wsclient.onmessage=onmessage
            //     wsclient.onclose=onclose
            // }, 3000);
        }
        


        var app = createApp({
            data() {
                return {
                    connectURL:"ws://127.0.0.1:9999/ws",
                    list:[
                    ],
                    uid:"登录态id",
                    recvid:"",
                    txt:"",
                    group:""
                }
            },
            methods:{
                connect(){
                    if(wsclient!=null){
                        wsclient.close()
                    }
                    wsclient = new WebSocket(this.connectURL)
                    wsclient.onmessage=onmessage
                    wsclient.onclose=onclose
                },
                sendtogroup(){
                    wsclient.send(JSON.stringify({
                        Id:"group_msg",
                        SendId:this.uid,
                        Type:"send_group",
                        Msg:{
                            Group:this.group,
                            Msg:this.txt
                        }
                    }))
                },
                joingroup(){
                    wsclient.send(JSON.stringify({
                        Id:"joingroup_msg",
                        SendId:this.uid,
                        Type:"joingroup",
                        Msg:{
                            Group:this.group
                        }
                    }))
                },
                leftgroup(){
                    wsclient.send(JSON.stringify({
                        Id:"joingroup_msg",
                        SendId:this.uid,
                        Type:"leftgroup",
                        Msg:{
                            Group:this.group,
                            Msg:this.txt
                        }
                    }))
                },
                ack(){
                    wsclient.send(JSON.stringify({
                        Id:"abc",
                        SendId:this.uid,
                        Type:"notify_ack",
                        Msg:{
                            RecvId:this.recvid,
                            Received:1
                        }
                    }))
                },
                sendmsg(){
                    wsclient.send(JSON.stringify({
                        Id:"abc",
                        SendId:this.uid,
                        Type:"send",
                        Msg:{
                            RecvId:this.recvid,
                            Msg:this.txt
                        }
                    }))
                }
            }
        }).mount('#app')
    </script>
</body>

</html>